<template>
  <view class="pageClass">
    <!-- 回退弹出层 -->
    <up-button
      type="error"
      text="回退"
      size="small"
      @click="handleCancel1"
      v-if="fromThatPage == '生产领料' || fromThatPage == '退库确认'"
    ></up-button>
    <button
      class="commonClass"
      @click="handleCancel1"
      style="background: #f56c6c"
      v-if="['领料确认', '出库确认', '退库申请详情'].includes(fromThatPage)"
    >
      回退
    </button>
    <up-popup :show="showReject" mode="bottom"
      ><!-- 操作按钮 -->
      <view class="btn-group">
        <text class="cancel" @click="showReject = false">取消</text>
        <text class="confirm" @click="handleSure">确定</text>
      </view>
      <view class="popup-content">
        <radio-group @change="groupChange">
          <view class="upflex10" style="flex-direction: column">
            <label
              class="radio"
              v-for="(item, index) in returnLists"
              :key="index"
            >
              <radio
                :value="item.workItemId"
                :checked="formObj.elRadio == item.workItemId"
              />
              {{ item.activityName }}
            </label>
          </view>
        </radio-group>
        <!-- 多行输入框 -->
        <view class="Reason">
          <view class="titleClass">回退原因</view>
          <up-textarea
            v-model="formObj.rollbackReason"
            placeholder="请输入回退原因（必填）"
          ></up-textarea>
        </view>
      </view>
    </up-popup>
  </view>
</template>

<script setup>
import {
  getflow,
  requisitionForProduction,
  ReturnrollbackConfirmation,
} from "../api.js";
const deprops = defineProps(["fromThatPage", "currentObj"]);
console.log(deprops.fromThatPage, "fromThatPage");
const Emits = defineEmits("fatherFn");
const showReject = ref(false);
const formObj = reactive({
  elRadio: "",
  activityName: "",
  rollbackReason: "",
});
const returnLists = ref([]);
const handleCancel1 = async () => {
  console.log(deprops.fromThatPage, "fromThatPage");
  formObj.elRadio = "";
  formObj.activityName = "";
  formObj.rollbackReason = "";
  try {
    const res = await getflow(deprops.currentObj.workflowWorkItemId);
    if (res) {
      returnLists.value = res || [];
      const obj = returnLists.value[0];
      if (obj) {
        formObj.elRadio = obj.workItemId;
        formObj.activityName = obj.activityName;
      }
    }
  } catch (err) {
    returnLists.value = [];
  }
  if (returnLists.value.length == 0) {
    return uni.showToast({
      icon: "none",
      title: "请联系管理员",
    });
  }
  showReject.value = true;
};
const groupChange = (e) => {
  console.log(e, "e");
  formObj.elRadio = e.detail.value;
  const obj = returnLists.value.find(
    (item) => item.workItemId == formObj.elRadio
  );
  formObj.activityName = obj ? obj.activityName : "";
  console.log(formObj, "formObj");
};
const handleSure = async () => {
  console.log(formObj, "formObj");
  if (!formObj.elRadio) {
    return uni.showToast({
      icon: "none",
      title: "请选择回退的工作流",
    });
  }
  if (!formObj.rollbackReason) {
    return uni.showToast({
      icon: "none",
      title: "请输入回退意见",
    });
  }
  //   const obj1 = {
  //     orderMaterialId: deprops.currentObj.orderMaterialId,
  //     workflowWorkItemId: deprops.currentObj.workflowWorkItemId,
  //     rollbackFromWorkitemId: formObj.elRadio,
  //     rollbackReason: formObj.rollbackReason,
  //     activityName: formObj.activityName,
  //   };
  const obj2 = {
    requirementId: deprops.currentObj.id,
    rollbackFromWorkitemId: formObj.elRadio,
    workflowWorkItemId: deprops.currentObj.workflowWorkItemId,
    rollbackReason: formObj.rollbackReason,
    activityName: formObj.activityName,
  };
  const obj3 = {
    returnOrderId: deprops.currentObj.id,
    rollbackFromWorkitemId: formObj.elRadio,
    workflowWorkItemId: deprops.currentObj.workflowWorkItemId,
    rollbackReason: formObj.rollbackReason,
    activityName: formObj.activityName,
  };
  //   if (deprops.fromThatPage == "技术评审") {
  //     await technicalreturnreview(obj1);
  //   } else if (deprops.fromThatPage == "生产评审") {
  //     await productionreturnreview(obj1);
  //   } else
  if (deprops.fromThatPage == "生产领料") {
    await requisitionForProduction(deprops.currentObj.id, obj2);
  } else if (deprops.fromThatPage == "领料确认") {
    await requisitionForProduction(deprops.currentObj.id, obj2);
  } else if (deprops.fromThatPage == "出库确认") {
    await requisitionForProduction(deprops.currentObj.id, obj2);
  } else if (
    deprops.fromThatPage == "退库确认" ||
    deprops.fromThatPage == "退库申请详情"
  ) {
    await ReturnrollbackConfirmation(deprops.currentObj.id, obj3);
  }
  showReject.value = false;
  Emits("fatherFn");
  uni.showToast({ title: "回退成功", icon: "success" });
};
</script>

<style lang="scss" scoped>
</style>
